﻿var express = require('express');
var jwt = require('jsonwebtoken');

//创建express子路由
var router = express.Router();

//导入User模型对象, 用来处理用户的登陆
var userModel = require("../models/userModel")
var commentsModel = require("../models/commentsModel")
var listModel = require("../models/listModel")
var addressModel = require("../models/addressModel")
var newsModel = require("../models/newsModel")
var commicationModel = require("../models/commicationModel")

router.post('/address', (req, res) => {
    console.log('address', req.body);
    var { address, name, latitude, longitude, title, author, neirong, time } = req.body;
    addressModel.insertMany({ address, name, latitude, longitude, title, author, neirong, time }, (err) => {
        if (!err) { //如果没有出错
            res.send({ code: 200, msg: "插入成功!" });
        }
    })
})
router.post('/addcomments', (req, res) => {
    console.log('req.body', req.body);
    var { title, author, neirong, time } = req.body;
    commentsModel.insertMany({ title, author, neirong, time }, (err) => {
        if (!err) { //如果没有出错
            res.send({ code: 200, msg: "插入成功!" });
        }
    })
})
//api接口,处理注册用户的请求
router.post('/register', (req, res) => {
    //req 请求对象
    console.log('req.body', req.body);
    var { phone, pass, question, answer, time } = req.body;
    //查询用户是否存在
    userModel.find({ phone }, (err, data) => {
        if (data.length != 0) { //用户存在
            res.send({ code: 400, msg: "用户已存在,注册失败!" });
        } else { //用户不存在, 插入新用户到数据表中
            userModel.insertMany({ phone, pass, question, answer, time }, (err) => {
                if (!err) { //如果没有出错
                    res.send({ code: 200, msg: "注册成功!" });
                }
            })
        }
    })
})
router.post('/addnews', (req, res) => {
    console.log('addnews', req.body);
    var { title, author, neirong, time, path } = req.body;
    newsModel.insertMany({ title, author, neirong, time, path }, (err) => {
        if (!err) { //如果没有出错
            res.send({ code: 200, msg: "插入成功!" });
        }
    })
})
router.post('/add', (req, res) => {
    console.log('add', req.body);
    var { title, author, neirong, time } = req.body;
    commicationModel.insertMany({ title, author, neirong, time }, (err) => {
        if (!err) { //如果没有出错
            res.send({ code: 200, msg: "插入成功!" });
        }
    })
})
router.get('/comlist', (req, res) => {
    commicationModel.find({}, (err, data) => {
        res.send({
            data: data
        })
    }).sort({ "_id": -1 })
})
//创建遍历全体数据的主页
router.get('/list', (req, res) => {
    listModel.find({}, (err, data) => {
        res.send({
            data: data
        })
    })
})
router.get('/addlist', (req, res) => {
    addressModel.find({}, (err, data) => {
        res.send({
            data: data
        })
    }).sort({ "time": -1 })
})
router.get('/zhanshi', (req, res) => {
    listModel.aggregate([{ $sample: { size: 12 } }], (err, data) => {
        res.send({
            data: data
        })
    })
})
router.get('/newslist', (req, res) => {
    newsModel.find({}, (err, data) => {
        res.send({
            data: data
        })
    }).sort({ "time": -1 })
})
router.get('/dele', (req, res) => {
    console.log('req.query', req.query);
    console.log('req.query', req.query._id);
    newsModel.deleteOne({ _id: req.query._id }, (err, data) => {
        res.send({
            data: data
        })
    })
})
router.get('/delec', (req, res) => {
    console.log('req.query', req.query);
    console.log('req.query', req.query._id);
    commentsModel.deleteOne({ _id: req.query._id }, (err, data) => {
        res.send({
            data: data
        })
    })
})
router.get('/wenku', (req, res) => {
    listModel.aggregate([{ $sample: { size: 80 } }], (err, data) => {
        res.send({
            data: data
        })
    })
})
// router.get('/search', (req, res) => {
//     //req 请求对象 , 从 req.query 获取get参数
//     //res 响应对象 , 通过 res.send 发送数据到客户端(浏览器)
//     console.log('req.query', req.body);

//     var { Chinesecharacters } = req.body;
//     //查询用户是否存在
//     listModel.find({ Chinesecharacters }, (err, data) => {
//         if (data==) { //用户存在
//             res.send({ code: 400, msg: "查询成功", data: data });
//         } else {
//             res.send({ msg: '查询失败，对象不存在' })
//         }
//     })
// })
router.post('/search', (req, res) => {
    console.log('req.body.Chinesecharacters', req.body);
    // var { Chinesecharacters } = req.body;
    listModel.find({ Chinesecharacters: req.body.Chinesecharacters }, (err, data) => {
        if (err) {
            console.log(err)
        }
        console.log(data)
        res.send(data)
    });
})
router.post('/chakan', (req, res) => {
    console.log('req.body._id', req.body._id);
    // var { Chinesecharacters } = req.body;
    commicationModel.find({ _id: req.body._id }, (err, data) => {
        if (err) {
            console.log(err)
        }
        console.log(data)
        res.send(data)
    });
})
router.post('/mycomments', (req, res) => {
    console.log('req.body', req.body);
    // var { Chinesecharacters } = req.body;
    commentsModel.find({ and: [{ author: req.body.author }, { title: req.body.title }] },(err, data) => {
        if (err) {
            console.log(err)
        }
        console.log(data)
        res.send(data)
    });
})


//实现模糊搜索
//db.UserInfo.find({userName: /A/})
router.post('/mohusearch', (req, res) => {
    console.log('req.body.title', req.body.title);
    // var { Chinesecharacters } = req.body;
    commicationModel.find({ title: {$regex:req.body.title} }, (err, data) => {
        if (err) {
            console.log(err)
        }
        console.log(data)
        res.send(data)
    });
})
router.post('/commentslist', (req, res) => {
    console.log('req.body', req.body.title);
    // var { Chinesecharacters } = req.body;
    commentsModel.find({ title: req.body.title }, (err, data) => {
        if (err) {
            console.log(err)
        } else {
            console.log(data)
        res.send(data)
        }
        
    }).sort({ "_id": -1 })
})
router.post('/zhaohui', (req, res) => {
    console.log('req.body.phone', req.body);
    // var { Chinesecharacters } = req.body;
    userModel.find({ phone: req.body.phone }, (err, data) => {
        if (err) {
            console.log(err)
        }
        console.log(data)
        res.send(data)
    });
})
router.post('/modefiy', (req, res) => {
    console.log('req.body', req.body);
    // var { Chinesecharacters } = req.body;
    userModel.updateOne({ phone: req.body.phone }, { $set: { pass: req.body.pass }, }, (err, data) => {
        if (err) {
            console.log(err)
        }
        console.log(data)
        res.send(data)
    });
})











//api接口,处理用户登陆的请求


router.get('/login', (req, res) => {
    //req 请求对象 , 从 req.query 获取get参数
    //res 响应对象 , 通过 res.send 发送数据到客户端(浏览器)
    console.log('req.query', req.query);

    var { phone, pass } = req.query;
    //查询用户是否存在
    userModel.find({ phone, pass }, (err, data) => {
        if (data.length == 1) { //用户存在,登陆成功
            var token = jwt.sign({ phone }, 'nodejs', { algorithm: 'HS256', expiresIn: '1h' });
            res.send({
                code: 200,
                msg: "登陆成功!",
                data: data,
                token
            });
        } else { //用户不存在,登陆失败
            res.send({ code: 400, msg: "登陆失败!" });
        }
    })
})


module.exports = router;